package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.utils.ThreadLocalUtils;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealVOList = setmealMapper.findByPage(setmealPageQueryDTO);
        Page<SetmealVO> pageObj = (Page<SetmealVO>) setmealVOList;
        return new PageResult(pageObj.getTotal(), pageObj.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        try {
            Setmeal setmeal = new Setmeal();
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            BeanUtils.copyProperties(setmealDTO, setmeal);
            setmeal.setCreateTime(LocalDateTime.now());
            setmeal.setCreateUser(ThreadLocalUtils.getLoginEmpId());
            setmealMapper.insertSetmeal(setmeal);
            if (setmealDishes != null && setmealDishes.size() > 0) {
                setmealDishes.forEach(setmealDish -> {
                    setmealDish.setSetmealId(setmeal.getId());
                });
                setmealDishMapper.insert(setmealDishes);
            }
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(List<Long> ids) {
        setmealMapper.deleteBatch(ids);
        setmealDishMapper.delete(ids);
    }

    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setmealMapper.findById(id);
        List<SetmealDish> dishes = setmealDishMapper.findDishBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishes);
        return  setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setUpdateUser(ThreadLocalUtils.getLoginEmpId());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.update(setmeal);
        setmealDishMapper.delete(List.of(setmeal.getId()));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public void updateStatus(Integer status,Long id) {
        setmealMapper.updateStatus(status,id);
    }
}
